package top.deepdesigner.controller.goods;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsComment;
import top.deepdesigner.service.DtsCommentService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.HashMap;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/24 星期六 14:29:03 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/comment")
public class CommentController {
  @Autowired
  private DtsCommentService commentService;

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品评论"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:comment:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params, @RequestParam(defaultValue = "") Integer userId, @RequestParam(defaultValue = "") Integer valueId) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");
    CommonListVO<DtsComment> commonListVO = new CommonListVO<>();
    //获取分页数量
    Long count = commentService.getCommentCount();
    List<DtsComment> commentList = commentService.getCommentList(page, limit, sort, order, userId, valueId);
    commonListVO.setTotal(count);
    commonListVO.setItems(commentList);
    return ResponseUtil.ok(commonListVO);
  }

  @RequiresPermissionsDesc(menu = {"商品管理", " 商品评论"}, button = "删除")
  @RequiresPermissions(value = {"admin:comment:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsComment dtsComment) {
    Boolean b = commentService.deleteComment(dtsComment);
    if (b) return ResponseUtil.ok();
    return ResponseUtil.fail();
  }
}
